package com.zlq.javaWeb3.filter;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class OnLineListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        System.out.println("创建");
        //获取ServletContext对象
        ServletContext application = se.getSession().getServletContext();
        Integer online = application.getAttribute("online")==null?null:(Integer) application.getAttribute("online");
        if (online == null){
            online=1;
            System.out.println(online);
        }else {
            online++;
            System.out.println(online);
        }
        application.setAttribute("online",online);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("销毁");
        ServletContext application = se.getSession().getServletContext();
        Integer online = application.getAttribute("online")==null?null:(Integer) application.getAttribute("online");
        if (online == null){
            online=1;
            System.out.println(online);
        }else {
            online++;
            System.out.println(online);
        }
        application.setAttribute("online",online);
    }
}
